/**
 * 
 */

var optMap = {
		"10086": "中国移动" ,
		"189" : "中国电信",
		"10010" : "中国联通"
};

jQuery.demonPlugin = {
	asyncData : function(method, dataurl, paramData, suc, err, headers){
		if (headers!=null)
			headers.datatype = "wifidemon";
		else 
			headers = {"datatype": "wifidemon"};
		$.ajax({
			type: method,
			dataType: "jsonp",
			headers: headers,
			url:  dataurl,
			data: paramData,
			success: suc ,
			error: err
			});
	},
	
	collectFormData : function(dataset){
		if (!dataset || !dataset instanceof Array)
			dataset = [];
		$("[formParam=formParam]").each(function(i, v){
			var val;
			if ( $(this).val() && (val=$(this).val()).trim() != "" )
				dataset.push({"name": $(this).attr("id"),
							 "value": val});
		});
		return dataset;
	},
	
	formValidate : function(id){
		var rules = {};
		var msgs = {};
		$("#"+id+" input").each(function(){
			var formName = $(this).attr("name");
			if (!formName)
				return;
			var rulestr = $(this).attr("check-type");
			if (!rulestr || rulestr=="")
				return;
			
			var ruleset = rulestr.split(" ");
			rules[formName] = {};
			msgs[formName] = {};
			for (var i in ruleset){
				if (ruleset[i]=="")
					continue;
				var msg = $(this).attr(ruleset[i] + "-message");
				var frule = $(this).attr(ruleset[i] + "-rule");
				rules[formName][ruleset[i]] = frule&&frule!=""?frule:true;
				if (msg && msg!="")
					msgs[formName][ruleset[i]] = $.demonPlugin.wrapHelpMsg(msg);
			}
		});
		$("#"+id).validate({
		    rules: rules,
			messages: msgs,
			highlight: function(element) {
					$(element).closest('.control-group').removeClass('success').addClass('error');
				},
			success: function(element) {
					element.addClass('valid')
					.closest('.control-group').removeClass('error').addClass('success');
				} 
		});
	},
	
	wrapHelpMsg : function(msg){
		return '<span class="help-inline" for="email">'+msg+'</span>';
	},
	
	initForm : function(formId, data, edit){
		var dom = formId? $("#"+formId) : $("body");
		for (var e in data){
			$("#"+e, dom).val(data[e]);
		}
	}
};

Date.prototype.format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function setHeight(){
	$("#contentFrame", window.parent.document).height(
			$(document.body).height()>800?$(document.body).height():800);
};

function getOpts(targetId, isCodeValue){
	$.demonPlugin.asyncData("GET", "opt/opts", null,function(data){
				if (data.status=="ok"){
					var sData = data.data;
					for (var e in sData){
						if (isCodeValue)
							$("<option>").val(sData[e].code).text(sData[e].name).appendTo("#"+targetId);
						else
							$("<option>").val(sData[e].name).text(sData[e].name).appendTo("#"+targetId);
					}
				}
			}
			);
};

function mTimeFormat(data, type, object){
	try{
		return data?new Date(data).format("yyyy-MM-dd hh:mm:ss"):data;
	}catch(err){
		return data;
	}
}